home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 104 / MacAddict_104_2005-04.iso / Software / Internet & Communication / WordPress 1.2.2 freeware.dmg / wordpress / wp-admin / upload.php < prev    next >
Encoding:
PHP Script  |  2004-12-16  |  8.4 KB  |  235 lines

  1. <?php
  2. require_once('../wp-includes/wp-l10n.php');
  3.  
  4. $title = 'Upload Image or File';
  5.  
  6. require_once('admin-header.php');
  7.  
  8. if ($user_level == 0) //Checks to see if user has logged in
  9.     die (__("Cheatin' uh ?"));
  10.  
  11. if (!get_settings('use_fileupload')) //Checks if file upload is enabled in the config
  12.     die (__("The admin disabled this function"));
  13.  
  14. $allowed_types = explode(' ', trim(strtolower(get_settings('fileupload_allowedtypes'))));
  15.  
  16. if ($_POST['submit']) {
  17.     $action = 'upload';
  18. } else {
  19.     $action = '';
  20. }
  21.  
  22. if (!is_writable(get_settings('fileupload_realpath')))
  23.     $action = 'not-writable';
  24. ?>
  25.  
  26. <div class="wrap">
  27.  
  28. <?php
  29. switch ($action) {
  30. case 'not-writable':
  31. ?>
  32. <p><?php printf(__("It doesn't look like you can use the file upload feature at this time because the directory you have specified (<code>%s</code>) doesn't appear to be writable by WordPress. Check the permissions on the directory and for typos."), get_settings('fileupload_realpath')) ?></p>
  33.  
  34. <?php
  35. break;
  36. case '':
  37.     foreach ($allowed_types as $type) {
  38.         $type_tags[] = "<code>$type</code>";
  39.     }
  40.     $i = implode(', ', $type_tags);
  41. ?>
  42. <p><?php printf(__('You can upload files with the extension %1$s as long as they are no larger than %2$s <abbr title="Kilobytes">KB</abbr>. If you’re an admin you can configure these values under <a href="%3$s">options</a>.'), $i, get_settings('fileupload_maxk'), 'options-misc.php') ?></p>
  43.     <form action="upload.php" method="post" enctype="multipart/form-data">
  44.     <p>
  45.       <label for="img1"><?php _e('File:') ?></label>
  46.       <br />
  47.     <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo get_settings('fileupload_maxk') * 1024 ?>" />
  48.     <input type="file" name="img1" id="img1" size="35" class="uploadform" /></p>
  49.     <p>
  50.     <label for="imgdesc"><?php _e('Description:') ?></label><br />
  51.     <input type="text" name="imgdesc" id="imgdesc" size="30" class="uploadform" />
  52.     </p>
  53.     
  54.     <p><?php _e('Create a thumbnail?') ?></p>
  55.     <p>
  56.     <label for="thumbsize_no">
  57.     <input type="radio" name="thumbsize" value="none" checked="checked" id="thumbsize_no" />
  58.     <?php _e('No thanks') ?></label>
  59.     <br />
  60.         <label for="thumbsize_small">
  61. <input type="radio" name="thumbsize" value="small" id="thumbsize_small" />
  62. <?php _e('Small (200px largest side)') ?></label>
  63.         <br />
  64.         <label for="thumbsize_large">
  65. <input type="radio" name="thumbsize" value="large" id="thumbsize_large" />
  66. <?php _e('Large (400px largest side)') ?></label>
  67.         <br />
  68.         <label for="thumbsize_custom">
  69.         <input type="radio" name="thumbsize" value="custom" id="thumbsize_custom" />
  70. <?php _e('Custom size') ?></label>
  71.       : 
  72.       <input type="text" name="imgthumbsizecustom" size="4" />
  73.     <?php _e('px (largest side)') ?>    </p>
  74.     <p><input type="submit" name="submit" value="<?php _e('Upload File') ?>" /></p>
  75.     </form>
  76. </div><?php 
  77. break;
  78. case 'upload':
  79.  
  80. //Makes sure they choose a file
  81.  
  82. //print_r($_FILES);
  83. //die();
  84.  
  85.  
  86.     $imgalt = basename( (isset($_POST['imgalt'])) ? $_POST['imgalt'] : '' );
  87.   
  88.     $img1_name = (strlen($imgalt)) ? $imgalt : basename( $_FILES['img1']['name'] );
  89.      $img1_name = preg_replace('/[^a-z0-9.]/i', '', strtolower($img1_name));
  90.     $img1_type = (strlen($imgalt)) ? $_POST['img1_type'] : $_FILES['img1']['type'];
  91.     $imgdesc = htmlentities2($imgdesc);
  92.  
  93.     $pi = pathinfo($img1_name);
  94.     $imgtype = strtolower($pi['extension']);
  95.  
  96.     if (in_array($imgtype, $allowed_types) == false) {
  97.         die(sprintf(__('File %1$s of type %2$s is not allowed.') , $img1_name, $imgtype));
  98.     }
  99.  
  100.     if (strlen($imgalt)) {
  101.         $pathtofile = get_settings('fileupload_realpath')."/".$imgalt;
  102.         $img1 = $_POST['img1'];
  103.     } else {
  104.         $pathtofile = get_settings('fileupload_realpath')."/".$img1_name;
  105.         $img1 = $_FILES['img1']['tmp_name'];
  106.     }
  107.  
  108.     // makes sure not to upload duplicates, rename duplicates
  109.     $i = 1;
  110.     $pathtofile2 = $pathtofile;
  111.     $tmppathtofile = $pathtofile2;
  112.     $img2_name = $img1_name;
  113.  
  114.     while (file_exists($pathtofile2)) {
  115.         $pos = strpos($tmppathtofile, '.'.trim($imgtype));
  116.         $pathtofile_start = substr($tmppathtofile, 0, $pos);
  117.         $pathtofile2 = $pathtofile_start.'_'.zeroise($i++, 2).'.'.trim($imgtype);
  118.         $img2_name = explode('/', $pathtofile2);
  119.         $img2_name = $img2_name[count($img2_name)-1];
  120.     }
  121.  
  122.     if (file_exists($pathtofile) && !strlen($imgalt)) {
  123.         $i = explode(' ', get_settings('fileupload_allowedtypes'));
  124.         $i = implode(', ',array_slice($i, 1, count($i)-2));
  125.         $moved = move_uploaded_file($img1, $pathtofile2);
  126.         // if move_uploaded_file() fails, try copy()
  127.         if (!$moved) {
  128.             $moved = copy($img1, $pathtofile2);
  129.         }
  130.         if (!$moved) {
  131.             die(sprintf(__("Couldn't upload your file to %s."), $pathtofile2));
  132.         } else {
  133.             chmod($pathtofile2, 0666);
  134.             @unlink($img1);
  135.         }
  136.  
  137.     // 
  138.     
  139.     // duplicate-renaming function contributed by Gary Lawrence Murphy
  140.     ?>
  141.     <p><strong><?php __('Duplicate File?') ?></strong></p>
  142.     <p><b><em><?php printf(__("The filename '%s' already exists!"), $img1_name); ?></em></b></p>
  143.     <p> <?php printf(__("Filename '%1\$s' moved to '%2\$s'"), $img1, "$pathtofile2 - $img2_name") ?></p>
  144.     <p><?php _e('Confirm or rename:') ?></p>
  145.     <form action="upload.php" method="post" enctype="multipart/form-data">
  146.     <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo  get_settings('fileupload_maxk') *1024 ?>" />
  147.     <input type="hidden" name="img1_type" value="<?php echo $img1_type;?>" />
  148.     <input type="hidden" name="img1_name" value="<?php echo $img2_name;?>" />
  149.     <input type="hidden" name="img1_size" value="<?php echo $img1_size;?>" />
  150.     <input type="hidden" name="img1" value="<?php echo $pathtofile2;?>" />
  151.     <input type="hidden" name="thumbsize" value="<?php echo $_REQUEST['thumbsize'];?>" />
  152.     <input type="hidden" name="imgthumbsizecustom" value="<?php echo $_REQUEST['imgthumbsizecustom'];?>" />
  153.     <?php _e('Alternate name:') ?><br /><input type="text" name="imgalt" size="30" class="uploadform" value="<?php echo $img2_name;?>" /><br />
  154.     <br />
  155.     <?php _e('Description:') ?><br /><input type="text" name="imgdesc" size="30" class="uploadform" value="<?php echo $imgdesc;?>" />
  156.     <br />
  157.     <input type="submit" name="submit" value="<?php _e('Rename') ?>" class="search" />
  158.     </form>
  159. </div>
  160. <?php 
  161.  
  162. require('admin-footer.php');
  163. die();
  164.  
  165.     }
  166.  
  167.     if (!strlen($imgalt)) {
  168.         @$moved = move_uploaded_file($img1, $pathtofile); //Path to your images directory, chmod the dir to 777
  169.         // move_uploaded_file() can fail if open_basedir in PHP.INI doesn't
  170.         // include your tmp directory. Try copy instead?
  171.         if(!$moved) {
  172.             $moved = copy($img1, $pathtofile);
  173.         }
  174.         // Still couldn't get it. Give up.
  175.         if (!$moved) {
  176.             die(sprintf(__("Couldn't upload your file to %s."), $pathtofile));
  177.         } else {
  178.             chmod($pathtofile, 0666);
  179.             @unlink($img1);
  180.         }
  181.         
  182.     } else {
  183.         rename($img1, $pathtofile)
  184.         or die(sprintf(__("Couldn't upload your file to %s."), $pathtofile));
  185.     }
  186.     
  187.     if($_POST['thumbsize'] != 'none' ) {
  188.         if($_POST['thumbsize'] == 'small') {
  189.             $max_side = 200;
  190.         }
  191.         elseif($_POST['thumbsize'] == 'large') {
  192.             $max_side = 400;
  193.         }
  194.         elseif($_POST['thumbsize'] == 'custom') {
  195.             $max_side = intval($_POST['imgthumbsizecustom']);
  196.         }
  197.         
  198.         $result = wp_create_thumbnail($pathtofile, $max_side, NULL);
  199.         if($result != 1) {
  200.             print $result;
  201.         }
  202.     }
  203.  
  204.  
  205.  
  206. if ( ereg('image/',$img1_type)) {
  207.     $piece_of_code = "<img src="". get_settings('fileupload_url') ."/$img1_name" alt="$imgdesc" />";
  208. } else {
  209.     $piece_of_code = "<a href="". get_settings('fileupload_url') . "/$img1_name" title="$imgdesc" />$imgdesc</a>";
  210. };
  211.  
  212. ?>
  213.  
  214. <h3><?php _e('File uploaded!') ?></h3>
  215. <p><?php printf(__("Your file <code>%s</code> was uploaded successfully!"), $img1_name); ?></p>
  216. <p><?php _e('Here’s the code to display it:') ?></p>
  217. <p><code><?php echo $piece_of_code; ?></code>
  218. </p>
  219. <p><strong><?php _e('Image Details') ?></strong>: <br />
  220. Name:
  221. <?php echo $img1_name; ?>
  222. <br />
  223. <?php _e('Size:') ?>
  224. <?php echo round($img1_size / 1024, 2); ?> <?php _e('<abbr title="Kilobyte">KB</abbr>') ?><br />
  225. <?php _e('Type:') ?>
  226. <?php echo $img1_type; ?>
  227. </p>
  228. </div>
  229. <p><a href="upload.php"><?php _e('Upload another') ?></a></p>
  230. <?php
  231. break;
  232. }
  233. include('admin-footer.php');
  234. ?>
  235.